package com.pmlt.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pmlt.common.lang.Result;
import com.pmlt.entity.Parking;
import com.pmlt.service.ParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Author:wen-tlp
 * @Date:2022/1/29 11:34
 * @Description:
 */

@RestController
@RequestMapping("/parking")
public class ParkingController {

    @Autowired
    ParkingService parkingService;

    // 查询所有车位信息
    @GetMapping("/parkings")
    public Result list() {
        IPage<Parking> page = new Page<>();
        return Result.succ(parkingService.page(page, new QueryWrapper<Parking>().orderByDesc("created")));
    }

    // 编辑车位信息
    @PostMapping("/edit")
    public Result edit(@Validated @RequestBody Parking parking) {
        Parking temp = null;
        if (parking.getId() != null) {
            parkingService.getById(parking.getId());
        }else {
            temp = new Parking();
            temp.setCreated(LocalDateTime.now());
        }
        BeanUtil.copyProperties(parking, temp, "id", "created");
        parkingService.saveOrUpdate(temp);
        return Result.succ(parking);
    }

}

